package com.vf.s.mvc.city.controller;

import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.vf.core.controller.BaseController;
import com.vf.core.util.UuidUtil;
import com.vf.s.common.model.city.BizCityMenu;
import com.vf.s.mvc.city.service.BizCityMenuService;

public class BizCityMenuController extends BaseController{
	
	protected final Log LOG = Log.getLog(getClass());
	
	@Inject
	private BizCityMenuService srv;

	public void index() {
		render("list.html");
	}
	
	public void edit() {
		render("edit.html");
	}
	
	public void getList() {
		List<BizCityMenu> menuList=srv.getChildrenAllTree(this.getPara("cityId"));
		if(menuList!=null&& menuList.size()>0) {
			renderSuccess("成功", menuList);
		}else {
			this.renderError("暂无菜单！");
		}
	}
	
	/***
	 * 保存
	 */
	public void save() {
		BizCityMenu menu = getModel(BizCityMenu.class);
		if (StrKit.notBlank(menu.getId())) {
			if (menu.update()) {
				renderSuccess("更新成功！");
			} else {
				renderError("更新失败！");
			}
		} else {
			menu.setId(UuidUtil.getUUID());
			if (menu.save()) {
				renderSuccess("保存成功！");
			} else {
				renderError("保存失败！");
			}
		}

	}

	/***
	 * 删除
	 * 
	 * @throws Exception
	 */
	public void delete() throws Exception {
		String id = getPara("id");
		List<BizCityMenu> list = srv.getChildrenByPid(id);
		if (list.size() <= 0) {
			BizCityMenu menu = BizCityMenu.dao.findById(id);
			if (menu != null) {
				if (menu.delete()) {
					renderSuccess("删除成功！");
				} else {
					renderError("删除失败！");
				}
			} else {
				renderError("数据不存在,请刷新后再试!");
			}
		} else {
			renderError("当前节点有子节点,不允许删除!");
			return;
		}
	}
    

}
